--通天塔头条
---@class Billboard_TowerPanel : UIComponentBase
local TowerPanel = Class("TowerPanel",UIComponentBase)
local t = TowerPanel

function t:OnCreate()
    self.rankList = self:GetList("ranklist")
    self.btnRank = self:GetChild("LookRank")
    self.btnReward = self:GetChild("RewardGet")
    self.changeTxt = self:GetChild("change")
    self.titleText = self:GetChild("title")
    self:RegisterOnClick(self.btnRank,self.ClickRank)
    self:RegisterOnClick(self.btnReward,self.ClickReward)
    self:SetDynamicList(self.rankList,self.ItemRender,self,true)
    self.playerDataList = {}
    self.rankList.numItems = 3
    self.rankType = logicMgr.NoticeManager.RankType.tower
end

function t:OnEnable()
    local tb = logicMgr.TowerManager.getRanks(1)
    table.clear(self.playerDataList)
    if tb and table.getn(tb) > 0 then
        for i = 1,3 do
            if  i<=#tb then
                table.insert(self.playerDataList,tb[i])
            else
                break
            end
        end
    end
    self.rankList.numItems = 3
    self.changeTxt.text = logicMgr.NoticeManager.GetAbnormalChangeStr(self.rankType)
    self.titleText.text = getLanguage("FunctionName303")
    self:RefreshReward()
end
function t:OnDisable()
end
function t:OnDestroy()
    self.playerDataList = nil
end
---@param go GComponent
function t:ItemRender(index,go)
    local data = self.playerDataList[index]
    local icon = go:GetChild("head"):GetChild("head"):GetChild("HeadIcon")
    local frame = go:GetChild("head"):GetChild("frame")
    go.onClick:Clear()
    if data then
        self:SetPlayerIcon(icon,data.headIcon,frame,data.headFrame)
        go:GetChild("Name").text = data.name

        self:RegisterOnClick(go,self.ClickIcon,self,index)
    else
        icon.url = EnumConst.AtlasPath.CommonUI.."icon_hero_kb91"
        frame.url = ""
        go:GetChild("Name").text = getLanguage("BillboardRankNeedPlayer")
    end
    go:GetController("ctrlRank").selectedIndex = index-1
end
function t:GetAllData()
    local tb = logicMgr.TowerManager.getRanks(1)
    table.clear(self.playerDataList)
    for i = 1,3 do
        if  i<=#tb then
            table.insert(self.playerDataList,tb[i])
        else
            break
        end
    end
    --无异动
    if not logicMgr.NoticeManager.HasAlertChange(self.rankType) then
        if #self.playerDataList == 0 then
            self.changeTxt.text = getLanguage("BillbardRankTowerNormalTip")
        else
            local playerName = self.playerDataList[1].name
            self.changeTxt.text = string.format(logicMgr.NoticeManager.GetAbnormalChangeStr(self.rankType),playerName)
        end
    end

    self.rankList.numItems = math.min(#self.playerDataList + 3,3)
    if #self.playerDataList == 0 then
        self.changeTxt.text = logicMgr.NoticeManager.GetChangeStr(self.rankType,NoticeManager.NO_MAIN_CHOOSE_TYPE)
    end
end

function t:RefreshReward()
    self.btnReward.visible = logicMgr.NoticeManager.HasReward(self.rankType )
end
function t:ClickRank()
    logicMgr.UIShowManager.ShowUITowerRank(1)
end

function t:ClickReward()
    NoticeHandler.ReqChangeAwardByType(self.rankType)
end
function t:ClickIcon(index)
    local data = self.playerDataList[index]
    if not data then return end

    roleInfoHandler.sendreqOtherRoleInfo(data.playerId);
end

return t